package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/tendermint/tendermint/abci/example/kvstore"
	"github.com/tendermint/tendermint/my_test/config"
	"github.com/tendermint/tendermint/my_test/utils"
	"github.com/tendermint/tendermint/rpc/client/http"
	"math/rand"
	"strconv"
	"time"
)

const (
	maxObjectId = 100
)

func main() {
	rand.Seed(time.Now().Unix())

	cli, err := http.New(config.DefaultIPAndPort, "/websocket")
	if err != nil {
		panic(err)
	}

	for i := 0; i < config.TxNum; i++ {
		objectId := rand.Intn(maxObjectId)
		objectIdString := strconv.Itoa(objectId)

		tx := &kvstore.Tx{
			OperationType: "PUT",
			OperatorID:    config.OperatorId,
			Time:          time.Now().String(),
			Data: &kvstore.FileData{
				Eigenvalue:   utils.RandomPoint(),
				ObjectID:     objectIdString,
				OwnerID:      objectIdString,
				FileCategory: "prescription_detail",
				FileType:     config.DataTypeList[objectId%3+1],
			},
			DataType: "FileData",
		}

		jsonTx, err := json.Marshal(tx)
		if err != nil {
			panic(err)
		}

		if _, err = cli.BroadcastTxAsync(context.TODO(), jsonTx); err != nil {
			panic(err)
		}

		fmt.Println(objectId, tx.Data.FileType)
	}
}
